package com.hongdee.insurance.car.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.insurance.car.entity.TCrmCarWarningConf;
import com.hongdee.insurance.car.service.ITCrmCarWarningConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by My on 2017/6/7.
 */
@RestController
@RequestMapping("/crm/car/warning")
public class TCrmCarWarningConfController {


    @Autowired
    private ITCrmCarWarningConfService crmCarWarningConfService;

    /**
     * 修改或添加车辆预警信息
     * @param uid
     * @param token
     * @param carWarningConf
     * @return
     * @throws Exception
     */
    @RequestMapping("/addAndUpd")
    @ResponseBody
    public ResultEntity addAndUpdCarWarningConf(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                @RequestParam(name = "token", defaultValue = "") String token,
                                                @RequestBody TCrmCarWarningConf carWarningConf) throws Exception {
        ResultEntity res=new ResultEntity();
        if(carWarningConf.getMin()>=carWarningConf.getMax()){
            throw new Exception("预警值设置错误，请调整后重新录入！！");
        }
        if("".equals(carWarningConf.getId()) || null==carWarningConf.getId()){
            crmCarWarningConfService.saveCarWarningConf(carWarningConf,uid);
        }else{
            crmCarWarningConfService.updCarWarningConf(carWarningConf,uid);
        }
        res.setMsg("保存成功！！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 分页查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public ResultEntity page(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "start", defaultValue = "") String start,
                             @RequestParam(name = "limit", defaultValue = "") String limit,
                             @RequestBody Map data) throws Exception {
        ResultEntity res=new ResultEntity();
        Map map=crmCarWarningConfService.page(data,uid,start,limit);
        res.setData(map);
        res.setMsg("查询成功！！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 预警信息详情
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res=new ResultEntity();
        Map conf=this.crmCarWarningConfService.info(id);
        res.setData(conf);
        res.setSuccess(true);
        return res;
    }



}
